﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using WebSite.Code.AlipayClass;


namespace WebSite.cn
{
    public partial class alipay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


           ///////////////////////以下参数是需要设置的相关配置参数,设置后不会更改的///////////////////////////
           AlipayConfig con = new AlipayConfig();
           string partner = con.Partner;
           string key = con.Key;
           string seller_email = con.Seller_email;
           string input_charset = con.Input_charset;
           string notify_url = con.Notify_url;
           string return_url = con.Return_url;
           string show_url = con.Show_url;
           string sign_type = con.Sign_type;

           ////////////////////////////////////////////////////////////////////////////////////////////////////

           ///////////////////////以下参数是需要通过下单时的订单数据传入进来获得////////////////////////////////
           //必填参数
           string out_trade_no = DateTime.Now.ToString("yyyyMMddHHmmss");  //请与贵网站订单系统中的唯一订单号匹配
           string subject = Request.Form["ordercode"];                      //订单名称,显示在支付宝收银台里的“商品名称”里,显示在支付宝的交易管理的“商品名称”的列表里。
           string body = "";                          //订单描述、订单详细、订单备注,显示在支付宝收银台里的“商品描述”里
           string total_fee = Request.Form["paymoney"];                    //订单总金额,显示在支付宝收银台里的“应付总额”里

           //扩展功能参数——默认支付方式
           string paymethod = "";                                          //默认支付方式,四个值可选：bankPay(网银); cartoon(卡通); directPay(余额); CASH(网点支付),初始值
           string defaultbank = "";                                        //默认网银代号,代号列表见http://club.alipay.com/read.php?tid=8681379 初始值
           string pay_mode = Request.Form["pay"];
           if (pay_mode == "directPay")
           {
               paymethod = "directPay";
           }
           else
           {
               paymethod = "bankPay";
               defaultbank = pay_mode;
           }


           //扩展功能参数——防钓鱼
           //请慎重选择是否开启防钓鱼功能
           //exter_invoke_ip、anti_phishing_key一旦被设置过,那么它们就会成为必填参数
           //建议使用POST方式请求数据
           string anti_phishing_key = "";                                  //防钓鱼时间戳
           string exter_invoke_ip = "";                                    //获取客户端的IP地址,建议：编写获取客户端IP地址的程序
           //如：
           //exter_invoke_ip = "";
           //anti_phishing_key = AlipayFunction.Query_timestamp(partner);  //获取防钓鱼时间戳函数

           //扩展功能参数——其他
           string extra_common_param = "";                                 //自定义参数,可存放任何内容（除=、&等特殊字符外）,不会显示在页面上
           string buyer_email = "";			                            //默认买家支付宝账号

           //扩展功能参数——分润(若要使用,请按照注释要求的格式赋值)
           string royalty_type = "";                                   //提成类型,该值为固定值：10,不需要修改
           string royalty_parameters = "";
           //提成信息集,与需要结合商户网站自身情况动态获取每笔交易的各分润收款账号、各分润金额、各分润说明。最多只能设置10条
           //各分润金额的总和须小于等于total_fee
           //提成信息集格式为：收款方Email_1^金额1^备注1|收款方Email_2^金额2^备注2
           //如：
           //royalty_type = "10";
           //royalty_parameters = "111@126.com^0.01^分润备注一|222@126.com^0.01^分润备注二";

           /////////////////////////////////////////////////////////////////////////////////////////////////////

           string totalprice = Request.Form["totalprice"];
           string totalscore = Request.Form["totalscore"];
           string chk1 = Request.Form["chk1"];
           string carid = Request.Form["carid"];
           string cname = Request.Form["cname"];
           string tel = Request.Form["tel"];
           string phone = Request.Form["phone"];
           string post = Request.Form["post"];
           string address = Request.Form["address"];
           string email = Request.Form["email"];
           string content = Request.Form["content"];
           string cname1 = Request.Form["cname1"];
           string tel1 = Request.Form["tel1"];
           string phone1 = Request.Form["phone1"];
           string post1 = Request.Form["post1"];
           string address1 = Request.Form["address1"];
           string email1 = Request.Form["email1"];
           string content1 = Request.Form["content1"];
           string pay = Request.Form["pay"];
           string paymoney = Request.Form["paymoney"];
           string ordercode = Request.Form["ordercode"];
            
           //构造请求函数,无需修改
           AlipayService aliService = new AlipayService(partner, seller_email, return_url, notify_url, show_url, out_trade_no, subject, body, total_fee, paymethod, defaultbank, anti_phishing_key, exter_invoke_ip, extra_common_param, buyer_email, royalty_type, royalty_parameters, key, input_charset, sign_type,
                                                         totalprice, totalscore, chk1, carid, cname, tel, phone, post, address, email, content, cname1, tel1, phone1, post1, address1, email1, content1, pay, paymoney, ordercode);

            string sHtmlText = aliService.Build_Form();

           //打印页面
            lbOut_trade_no.Text = subject;
           lbTotal_fee.Text = total_fee;
           lbButton.Text = sHtmlText;


        }

        private string GetSendUrl()
        {
            string strsellerEmail = System.Configuration.ConfigurationManager.AppSettings["AliPayAccount"];         //卖家支付宝帐号
            string strAc = "poiouuytyLKKHGHTghII+-OGHbnmhgFHj124UIGY";        　　　　//卖家支付宝安全校验码
            string INTERFACE_URL = "https://www.alipay.com/payto:";
            string strCmd = "";           //命令字

            string totalprice = Request.Form["totalprice"];
            string totalscore = Request.Form["totalscore"];
            string chk1 = Request.Form["chk1"];
            string carid = Request.Form["carid"];
            string cname = Request.Form["cname"];
            string tel = Request.Form["tel"];
            string phone = Request.Form["phone"];
            string post = Request.Form["post"];
            string address = Request.Form["address"];
            string email = Request.Form["email"];
            string content = Request.Form["content"];
            string cname1 = Request.Form["cname1"];
            string tel1 = Request.Form["tel1"];
            string phone1 = Request.Form["phone1"];
            string post1 = Request.Form["post1"];
            string address1 = Request.Form["address1"];
            string email1 = Request.Form["email1"];
            string content1 = Request.Form["content1"];
            string pay = Request.Form["pay"];


            string str2CreateAc = ""
                            +  "totalprice=" + totalprice
                            +  "totalscore=" + totalscore
                            +  "chk1=" + chk1
                            +  "carid=" + carid
                            +  "cname=" + Server.UrlEncode(cname)
                            +  "tel=" + tel
                            +  "phone=" + phone
                            +  "post=" + post
                            +  "address=" + Server.UrlEncode(address)
                            +  "email=" + Server.UrlEncode(email)
                            +  "content=" + Server.UrlEncode(content)
                            +  "cname1=" + Server.UrlEncode(cname1)
                            +  "tel1=" + tel1
                            +  "phone1=" + phone1
                            +  "post1=" + post1
                            +  "address1=" + Server.UrlEncode(address1)
                            +  "email1=" + Server.UrlEncode(email1)
                            +  "content1=" + Server.UrlEncode(content1)
                            +  "pay=" + pay
                            +strAc;


            string acCode = GetMD5(str2CreateAc);

            string parameter = ""
                            + INTERFACE_URL + strsellerEmail + "?cmd=" + strCmd
                            + "&totalprice=" + totalprice
                            + "&totalscore=" + totalscore
                            + "&chk1=" + chk1
                            + "&carid=" + carid
                            + "&cname=" + Server.UrlEncode(cname)
                            + "&tel=" + tel
                            + "&phone=" + phone
                            + "&post=" + post
                            + "&address=" + Server.UrlEncode(address)
                            + "&email=" + Server.UrlEncode(email)
                            + "&content=" + Server.UrlEncode(content)
                            + "&cname1=" + Server.UrlEncode(cname1)
                            + "&tel1=" + tel1
                            + "&phone1=" + phone1
                            + "&post1=" + post1
                            + "&address1=" + Server.UrlEncode(address1)
                            + "&email1=" + Server.UrlEncode(email1)
                            + "&content1=" + Server.UrlEncode(content1)
                            + "&pay=" + pay
                            + "&acCode=" + acCode;   

            return parameter;
        }

        private static string GetMD5(string s)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] t = md5.ComputeHash(System.Text.Encoding.GetEncoding("gb2312").GetBytes(s));
            System.Text.StringBuilder sb = new System.Text.StringBuilder(32);
            for (int i = 0; i < t.Length; i++)
            {
                sb.Append(t[i].ToString("x").PadLeft(2, '0'));
            }
            return sb.ToString();
        }

    }
}